﻿using MetroDataApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MetroDataApi.Controllers.Management
{
    public class AllProjects1Controller : ApiController
    {

        public HttpResponseMessage Get(int prjId)
        {
            using (var db = new MetroDataDataContext())
            {
                var list = (from p in db.Projects
                            where p.PrjId <= 40
                            select p).ToList();
                var jsonSerializerSettings = new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                };
                var ignored = JsonConvert.SerializeObject(list, Formatting.Indented, jsonSerializerSettings);

                var response = Request.CreateResponse(HttpStatusCode.OK, list);
                //response.Content = new StringContent(ignored);
                return response;
            }
        }

        // GET api/allprojects1
        public string Get()
        {
            using (var db = new MetroDataDataContext())
            {
                var prjTypes = (from p in db.ProjectTypeEnum
                                select p).ToDictionary(p => p.ProjectType, t => t.Name);
                var list = from p in db.Projects
                           join q in db.MonitorProjectExtraInfo
                           on p.PrjId equals q.PrjId
                           into gs
                           from g in gs.DefaultIfEmpty()
                           orderby p.LineId
                           select new
                           {
                               PrjId = p.PrjId,
                               Name = p.Name,
                               LineId = p.LineId,
                               LineName = db.Lines.SingleOrDefault(t => t.Id == p.LineId).Name,
                               Remark = p.Remark,
                               Status = p.Status,
                               Location = db.Lines.SingleOrDefault(t => t.Id == p.LineId).Locate,
                               PrjSummary = g != null ? g.PrjSummary : null,
                               PrjLevel = g != null ? g.PrjLevel : "特级",
                               TunnelType = g != null ? g.TunnelType : "单圆隧道",
                               ConstructionCorp = g != null ? g.ConstructionCorp : "上勘院",
                               PrjState = g != null ? g.PrjState : "开挖",
                               ProjectTypeCode = p.ProjectType,
                               ProjectType = prjTypes[p.ProjectType],
                               OwnerCorp = g.OwnerCorp,
                               Department = g.Department
                           };

                var jsonSerializerSettings = new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };
                string ignored = JsonConvert.SerializeObject(list, Formatting.Indented, jsonSerializerSettings);
                return ignored;
            }

        }


    }
}
